<?php

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;

/**
 * @var string $title
 * @var string $data_folder
 * @var array $media_folders
 * @var string $media_folder
 * @var string $subfolders
 */
?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), $title]]) ?>

<h1><?= $title ?></h1>

<form method="post" action="<?= e(route('admin-media-select')) ?>" id="admin-media-form">
    <?= csrf_field() ?>

    <table class="table table-bordered table-sm">
        <thead>
            <tr>
                <th><?= I18N::translate('Media files') ?></th>
                <th><?= I18N::translate('Media folders') ?></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <label>
                        <input type="radio" name="files" value="local" <?= $files === 'local' ? 'checked' : '' ?>>
                        <?= /* I18N: “Local files” are stored on this computer */ I18N::translate('Local files') ?>
                    </label>
                    <br>
                    <label>
                        <input type="radio" name="files" value="external" <?= $files === 'external' ? 'checked' : '' ?>>
                        <?= /* I18N: “External files” are stored on other computers */ I18N::translate('External files') ?>
                    </label>
                    <br>
                    <label>
                        <input type="radio" name="files" value="unused" <?= $files === 'unused' ? 'checked' : '' ?>>
                        <?= I18N::translate('Unused files') ?>
                    </label>
                </td>
                <td>
                    <?php if ($files === 'local' || $files === 'unused') : ?>
                        <div class="input-group mb-2" dir="ltr">
                            <div class="input-group-prepend">
                                <div class="input-group-text"><?= e($data_folder) ?></div>
                            </div>
                                <?php if ($media_folders->count() > 1) : ?>
                                    <?= view('components/select', ['name' => 'media_folder', 'selected' => $media_folder, 'options' => $media_folders]) ?>

                                <?php else : ?>
                                    <input aria-label="<?= I18N::translate('Media folder') ?>"
                                           type="text" class="form-control" name="media_folder" value="<?= e($media_folder) ?>" readonly>
                                <?php endif ?>
                        </div>

                        <label>
                            <input type="radio" name="subfolders" value="include" <?= $subfolders === 'include' ? 'checked' : '' ?>>
                            <?= I18N::translate('Include subfolders') ?>
                        </label>
                        <br>
                        <label>
                            <input type="radio" name="subfolders" value="exclude" <?= $subfolders === 'exclude' ? ' checked' : '' ?>>
                            <?= I18N::translate('Exclude subfolders') ?>
                        </label>

                    <?php elseif ($files === 'external') : ?>
                        <?= I18N::translate('External media files have a URL instead of a filename.') ?>
                        <input type="hidden" name="media_folder" value="<?= e($media_folder) ?>">

                    <?php endif ?>
                </td>
            </tr>
        </tbody>
    </table>
</form>
<br>
<br>

<table
    class="table table-bordered table-sm datatables d-none"
    <?= view('lists/datatables-attributes') ?>
    data-ajax="<?= e(route('admin-media-data', ['files' => $files, 'media_folder' => $media_folder, 'subfolders' => $subfolders])) ?>"
    data-server-side="true"
>
    <thead>
        <tr>
            <th>
                <?= I18N::translate('Media file') ?>
            </th>

            <th data-sortable="false">
                <?= I18N::translate('Media') ?>
            </th>

            <th data-sortable="<?= $files === 'unused' ? 'false' : 'true' ?>">
                <?= I18N::translate('Media object') ?>
            </th>
        </tr>
    </thead>
</table>
<?= view('modals/create-media-from-file') ?>

<?php View::push('javascript') ?>
<script>
    $("#admin-media-form input, #admin-media-form select").change(function () {
        $("#admin-media-form").submit();
    });
</script>
<?php View::endpush() ?>
